package utils
{
import flash.events.MouseEvent;

import mx.controls.CheckBox;
import mx.controls.DataGrid;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.mx_internal;

use namespace mx_internal;
/** 
 *  The Renderer.
 */
public class CheckBoxHeaderRenderer extends CheckBox
{

	public function CheckBoxHeaderRenderer()
	{
		//focusEnabled = false;
		//this.toggle = true ;
	}

    private var _data:DataGridColumn ;
    
    private var _parentDG:DataGrid ;
    
    
    override public function get data():Object{
    	return _data ;
    }
	override public function set data(value:Object):void
	{
		_data = value as DataGridColumn;
		if(_data.headerText == "true"){
			this.selected = true;
		}else{
			this.selected = false ;
		}
		invalidateProperties();
		
	}
	
	
	override protected function commitProperties():void
	{
		super.commitProperties();
		//this.selected = this.selected ;
		//if (owner is ListBase){
			//selected = ListBase(owner).isItemSelected(data);
		//}
	} 

	/* eat keyboard events, the underlying list will handle them */
/* 	override protected function keyDownHandler(event:KeyboardEvent):void
	{
		super.keyDownHandler(event);
	} */

	/* eat keyboard events, the underlying list will handle them */
	/* override protected function keyUpHandler(event:KeyboardEvent):void
	{
		super.keyUpHandler(event);
	} */
    private var selectedIndices:Array = [];
	/* eat mouse events, the underlying list will handle them */
	 override protected function clickHandler(event:MouseEvent):void
	{
		super.clickHandler(event);
		
		_parentDG = super.owner as DataGrid ;
		 
		if (_parentDG.dataProvider == null || _parentDG.dataProvider.length == 0) {
			return;
		}
		//setSelected(!selected);
		if(this.selected){
			//this.selected = false ;
			selectedIndices = [] ;
			for(var i:int =0 ;i<_parentDG.dataProvider.source.length ;i++){
				selectedIndices.push(i);
			}
			_parentDG.selectedIndices = selectedIndices;
			this._data.headerText ="true";
		}else{
			//this.selected = true ;
			_parentDG.selectedItems = [] ;
			this._data.headerText ="false";
		} 
		//event.stopImmediatePropagation();
	} 

	/* center the checkbox if we're in a datagrid */
	override protected function updateDisplayList(w:Number, h:Number):void
	{
		super.updateDisplayList(w, h);

		/*  if (listData is DataGridListData)
		{
			var n:int = numChildren;
			for (var i:int = 0; i < n; i++)
			{
				var c:DisplayObject = getChildAt(i);
				if (!(c is TextField))
				{
					c.x = (w - c.width) / 2;
					c.y = 0;
				}
			}
		}  */
	}
	
	/* override public function validateProperties():void
	{
		super.validateProperties();
		if (_data)
		{
			this.label = this._data.headerText ;
			
		}
	} */

}

}